package com.designPattern.proxy.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Auther: zwg
 * @Date: 2019/2/21 16:07
 * @Description:
 */
public class CGLibProxy implements MethodInterceptor {

    private static CGLibProxy cgLibProxy = new CGLibProxy();

    public static CGLibProxy getInstance(){
        return cgLibProxy;
    }

    @SuppressWarnings("unchecked")
    public <T> T getProxy(Class<T> cls){
        return (T) Enhancer.create(cls, this);
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy proxy) throws Throwable {
        before();
        Object result = proxy.invokeSuper(o, objects);
        after();
        return result;
    }
    private void before() {
        System.out.println("Before...");
    }

    private void after() {
        System.out.println("After...");
    }
}
